<?php
include 'include/functions.php';
//set session save path
	session_save_path($session_path);
	// Initialize a session. 
	session_start();
	$count++;
	$sessionId = session_id();
	
	if(!$userID)
	{
		header("Location: logoff.php"); /* Redirect browser */
		/* Make sure that code below does not get executed when we redirect. */
		exit;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
  <head>
	<title>JoshHughes.com Football Pool</title>
		<!-- Style Sheets -->
		<link rel="stylesheet" href="http://football.joshhughes.com/css/football.css" type="text/css">
		<!-- Script Includes -->
		<script src="http://football.joshhughes.com/scripts/prototype.js" type="text/javascript"></script>
		<script src="http://football.joshhughes.com/scripts/scriptaculous.js" type="text/javascript"></script>
		<script src="http://football.joshhughes.com/scripts/football.js" type="text/javascript"></script>
	</head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<?php

if ($userID = 1) 
{
	if (($League == NULL)||($League ==""))
		$League = "NFL";
	//Connect To Database
	dbConnect();

	//check to see if a Weeknum value was passed, if not, set WeekNum to CurrWeek.
	if (($Weeknum == NULL) || ($Weeknum == "")) {
		$Weeknum = getWeek($League);
	}
	
	//check to make sure $Weeknum > 1 AND $Weeknum < 21
	$Weeknum = checkWeekBounds($Weeknum);

	?>
	
	<form action="<? echo $PHP_SELF; ?>" method="POST" name="WeekForm">
	<table bgcolor=#FFFFFF width="500" align="center"><tr>
	<tr><td colspan="7" align="center"><H1><? echo "$League Week $Weeknum"; ?></H1></td>
	<td align="right"><a href="<? echo $PHP_SELF; ?>?Weeknum=<? echo $Weeknum-1; ?>" class="permLink">Prev</a> | <a href="<? echo $PHP_SELF; ?>?Weeknum=<? echo $Weeknum+1; ?>" class="permLink">Next</a> &nbsp; or &nbsp; 
		<B>Go to:</B>
		<?
			WeekDropDown()
		?>
	</td>
	</tr>	
	<tr><TH>Visitor</TH><TH>&nbsp;</TH><TH>Home</TH><TH>Winner</TH></tr>
	<?
    if($Submit == "Set Winners")
	{
		echo "<tr><td colspan=\"4\">";
		if($Game1 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game1."' WHERE ((GameNum = 1) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game1 != "")
		if($Game2 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game2."' WHERE ((GameNum = 2) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game2 != "")
		if($Game3 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game3."' WHERE ((GameNum = 3) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game3 != "")
		if($Game4 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game4."' WHERE ((GameNum = 4) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game4 != "")
		if($Game5 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game5."' WHERE ((GameNum = 5) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game5 != "")
		if($Game6 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game6."' WHERE ((GameNum = 6) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game6 != "")
		if($Game7 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game7."' WHERE ((GameNum = 7) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game7 != "")
		if($Game8 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game8."' WHERE ((GameNum = 8) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game8 != "")
		if($Game9 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game9."' WHERE ((GameNum = 9) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game9 != "")
		if($Game10 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game10."' WHERE ((GameNum = 10) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game10 != "")
		if($Game11 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game11."' WHERE ((GameNum = 11) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game11 != "")
		if($Game12 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game12."' WHERE ((GameNum = 12) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game12 != "")
		if($Game13 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game13."' WHERE ((GameNum = 13) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game13 != "")
		if($Game14 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game14."' WHERE ((GameNum = 14) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game14 != "")
		if($Game15 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game15."' WHERE ((GameNum = 15) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game15 != "")
		if($Game16 != "") {
			mysql_query ("UPDATE ".$League."Schedule Set PickEmailed = '1', Winner = '".$Game16."' WHERE ((GameNum = 16) AND (Week = ".$Weeknum."))") or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game16 != "")
		if($MagicNum != "0") {
			mysql_query ("INSERT IGNORE INTO ".$League."WeeklyWinner (Week) Values ($Weeknum)") or die("Cannot query the database.<br>" . mysql_error());
			mysql_query ("UPDATE ".$League."WeeklyWinner Set MagicNum = '".$MagicNum."' WHERE Week = ".$Weeknum) or die("Cannot query the database.<br>" . mysql_error());
		}//end if($Game16 != "")
		 echo "Winners updated.</td></tr></table>";

	}
	else
	{	
		//Get all game info from $Weeknum.
		$SQL = "SELECT GameNum, Away, Home, Winner FROM ".$League."Schedule WHERE ((Week = ".$Weeknum.") AND (Winner = '-')) ORDER BY GameNum";
		$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
		do{
			if($myrow["Away"] != "")
			{
				echo "<tr><td>".$myrow["Away"]."</td><td> @ </td><td>".$myrow["Home"]."</td><td>";
				if ($myrow["Winner"] != "-")
				{
					echo "<B>".$myrow["Winner"]."</B></td></tr>";
				}
				else
				{
					echo "<select name=\"Game".$myrow["GameNum"]."\" class=\"select\"><option selected></option><option value=\"".$myrow["Away"]."\">".$myrow["Away"]."</option><option value=\"".$myrow["Home"]."\">".$myrow["Home"]."</option></select></td></tr>";
				}
			}
		}while($myrow = mysql_fetch_array($result));
		?>
			<tr><td colspan="4" align="center">Magic Number: <input type="text" id="MagicNum" name="MagicNum" value="0" size="3"></td></tr>
			<tr><td colspan="4" align="center"><input type="Submit" name="Submit" value="Set Winners"></td></tr>
			</table>
			<input type="hidden" name="Weeknum" value="<?=$Weeknum?>">
			<input type="hidden" name="League" value="<?=$League?>">
			</form>
		<?
     }//end if($Submit == "Set Winners")
}
else	//is $userID != 1
{
	?>
		<SCRIPT language="JavaScript">
			<!--
				window.location="logoff.php";
			//-->
		</SCRIPT>
	<?
} //end if ($userID == 1)
?>
			
			</div>
			<div id="leftcol" >
			
				<?PHP include_once("layout/left_column.php"); ?>
		
			</div>
			
		</div>
		
		<div id="footer" >
		
			<?PHP include_once("layout/footer.html"); ?>
			
		</div>
</div>
  </body>
</html>